﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo069
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Items.Add("省1");
                DropDownList1.Items.Add("省2");
                DropDownList1.Items.Add("省3");
                DropDownList2.Items.Add("市1");
                DropDownList2.Items.Add("市2");
                DropDownList2.Items.Add("市3");
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Province = DropDownList1.SelectedValue;
            DropDownList2.Items.Clear();
            if (Province == "省1")
            {
                DropDownList2.Items.Add("市1");
                DropDownList2.Items.Add("市2");
                DropDownList2.Items.Add("市3");
            }
            else if (Province == "省2")
            {
                DropDownList2.Items.Add("市3");
                DropDownList2.Items.Add("市4");
                DropDownList2.Items.Add("市5");
            }
            else
            {
                DropDownList2.Items.Add("市5");
                DropDownList2.Items.Add("市6");
                DropDownList2.Items.Add("市7");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //设置一个变量
            string hobby = null;
            //遍历多选列表的选项集合
            for (int i = 0; i < hobbys.Items.Count; i++)
            {
                //判断该选项是否被选中
                if (hobbys.Items[i].Selected)
                {
                    //将被选中的选项添加到变量中
                    hobby += hobbys.Items[i] + " ";
                }
            }
            string sex = null;
            foreach (ListItem item in RadioButtonList1.Items)
            {
                if (item.Selected)
                {
                    sex += item.Text+" "+item.Value;
                }
            }
            result.Text = $"姓名：{username.Text}<br>性别：{sex}<br>爱好：{hobby}<br>籍贯：{DropDownList1.SelectedValue}{DropDownList2.SelectedValue}<br>其它信息：{other.Text}<br>";
            
            //根据id.HasFile判断是否上传了文件
            if (ful.HasFile)
            {
                //获取文件名
                string fileName = ful.FileName;
                //判断文件是否为图片
                if (fileName.EndsWith(".jpg") || fileName.EndsWith(".png") || fileName.EndsWith(".jpeg"))
                {
                    //获取文件的绝对路径
                    string url = Server.MapPath("/img/" + fileName);
                    //保存文件
                    ful.SaveAs(url);
                    //设置图片控件显示目录
                    Image1.ImageUrl = "/img/" + fileName;
                    //设置图片为 显式的  显示在页面上
                    Image1.Visible = true;
                }
            }
        }
    }
}